<?php

namespace App\Actions\Grid;

use App\Models\ExhibitorInformation;
use Encore\Admin\Actions\Action;
use Encore\Admin\Grid;

class TotalRows extends Action
{
    protected $selector = '.rows-count';
    protected $grid;
    protected $btn_name = '总计';

    public function __construct(Grid $grid, $btn_name = null)
    {
        $this->grid = $grid;
        if ($btn_name) {
            $this->btn_name = $btn_name;
        }
    }

    public function html()
    {
        $count = $this->grid->model()->eloquent()->total();
        $exhibitor_information = $this->grid->model()->eloquent();
        $data = [];

        foreach ($exhibitor_information as $item) {
            $exhibitor_information_id = $item->id;
//            $exhibitor_information_id= $item->appellation;
            if (!isset($data[$exhibitor_information_id])) {
                $data[$exhibitor_information_id]['area'] = $item->area;
                $data[$exhibitor_information_id]['booth_fee'] = $item->booth_fee;
                $data[$exhibitor_information_id]['sponsorship_fees'] = $item->sponsorship_fees;
                $signing_status = $item->signing_status;

                if ($signing_status == 1) {
                    $data[$exhibitor_information_id]['signed_area'] = $item->area;
                    $data[$exhibitor_information_id]['signed_booth_fee'] = $item->booth_fee;
                    $data[$exhibitor_information_id]['signed_sponsorship_fees'] = $item->sponsorship_fees;
                } else {
                    $data[$exhibitor_information_id]['signed_area'] = 0;
                    $data[$exhibitor_information_id]['signed_booth_fee'] = 0;
                    $data[$exhibitor_information_id]['signed_sponsorship_fees'] = 0;
                }

                $collection_status = $item->collection_status;

                if ($collection_status == 1) {
                    $data[$exhibitor_information_id]['collection_area'] = $item->area;
                    $data[$exhibitor_information_id]['collection_booth_fee'] = $item->booth_fee;
                    $data[$exhibitor_information_id]['collection_sponsorship_fees'] = $item->sponsorship_fees;
                } else {
                    $data[$exhibitor_information_id]['collection_area'] = 0;
                    $data[$exhibitor_information_id]['collection_booth_fee'] = 0;
                    $data[$exhibitor_information_id]['collection_sponsorship_fees'] = 0;
                }
            }

        }

        // 假设我们要根据'score'字段求和
        $scores = array_column($data, 'area');
        $area = array_sum($scores);//展位面积
        $scores = array_column($data, 'booth_fee');
        $booth_fee = array_sum($scores);//展位费用
        $scores = array_column($data, 'sponsorship_fees');
        $sponsorship_fees = array_sum($scores);//赞助费

        $scores = array_column($data, 'signed_area');
        $signed_area_count = count($scores);//已签约总数

        $scores = array_column($data, 'signed_area');
        $signed_area = array_sum($scores);//已签约面积

        $scores = array_column($data, 'signed_booth_fee');
        $signed_booth_fee = array_sum($scores);//已签约费用

        $scores = array_column($data, 'signed_sponsorship_fees');
        $signed_sponsorship_fees = array_sum($scores);//已签约赞助费

        $scores = array_column($data, 'collection_area');
        $collection_area_count = count($scores);//已收款总数

        $scores = array_column($data, 'collection_area');
        $collection_area = count($scores);//已收款面积

        $scores = array_column($data, 'collection_booth_fee');
        $collection_booth_fee = count($scores);//已收款费用

        $scores = array_column($data, 'collection_sponsorship_fees');
        $collection_sponsorship_fees = count($scores);//已收款赞助费

        return <<<HTML
        <div class="top-count-box" style="position: fixed;bottom: 50px;">
            <a class="btn btn-sm btn-default">查询到：{$count}展商，展位面积：{$area}平，展位费：{$booth_fee}元，赞助费：{$sponsorship_fees}元</a>
            <a class="btn btn-sm btn-default">已签约：{$signed_area_count}家，面积：{$signed_area}平，展位费：{$signed_booth_fee}元，赞助费：{$signed_sponsorship_fees}元</a>
            <a class="btn btn-sm btn-default">已收款：{$collection_area_count}家，面积：{$collection_area}平，展位费：{$collection_booth_fee}元，赞助费：{$collection_sponsorship_fees}元</a>
        </div>
HTML;
    }
}
